package gopensocial.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Window;

public abstract class GOpenSocial {
	
	static GOpenSocial singleton;

	
	public static GOpenSocial getInstance(){
		return singleton;
	}
	
	public static void setInstance(GOpenSocial instance){
		singleton = instance;
	}

	/*
	 * 
	 * Get Viewer
	 * 
	 * */

	public native void getViewer()/*-{
		  var req = $wnd.opensocial.newDataRequest();
	      req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
	      req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');

		  req.send(this.@gopensocial.client.GOpenSocial::onLoadGetViewer(Lcom/google/gwt/core/client/JavaScriptObject;));
	}-*/;

	public native void getPeople(String id)/*-{
	  var req = $wnd.opensocial.newDataRequest();
      req.add(req.newFetchPersonRequest(id), 'viewer');

	  req.send(this.@gopensocial.client.GOpenSocial::onLoadGetViewer(Lcom/google/gwt/core/client/JavaScriptObject;));
    }-*/;

	public native void onLoadGetViewer(JavaScriptObject data) /*-{
	
	var singleton = @gopensocial.client.GOpenSocial::getInstance()();
	
    if(!data.hadError()){
        var viewer = data.get('viewer').getData();
        var name = viewer.getDisplayName();
        var id = viewer.getId();

        
		singleton.@gopensocial.client.GOpenSocial::createViewer(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(name, "null", id);
    }
    else {
        singleton.@gopensocial.client.GOpenSocial::onFail()();
    }
    }-*/;

	
	public void createViewer(String name, String picture, String id){
		Person person = createPerson(name, picture, id);
		onGetViewer(person);
	}

	
	public void onGetViewer(Person person) {
	}

	
	/*
	 * 
	 * Get Friends
	 * 
	 */
	
	public native void getFriends()/*-{
	  var req = $wnd.opensocial.newDataRequest();
      req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');

	  req.send(this.@gopensocial.client.GOpenSocial::onLoadGetFriends(Lcom/google/gwt/core/client/JavaScriptObject;));
}-*/;


	
	
	
	
	public native void onLoadGetFriends(JavaScriptObject data) /*-{
	
	var singleton = @gopensocial.client.GOpenSocial::getInstance()();
	
    if(!data.hadError()){
        var friends = data.get('viewerFriends').getData();
        var jFriends = singleton.@gopensocial.client.GOpenSocial::getNewList()();
        friends.each(function(person) {
           var name = person.getDisplayName();
           var id = person.getId();
        
           var jPerson = singleton.@gopensocial.client.GOpenSocial::createPerson(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(name, "null", id);

         	singleton.@gopensocial.client.GOpenSocial::addPerson(Ljava/util/List;Lgopensocial/client/Person;)(jFriends, jPerson);
         });

       	singleton.@gopensocial.client.GOpenSocial::onGetFriends(Ljava/util/List;)(jFriends);

    }
    else {
        singleton.@gopensocial.client.GOpenSocial::onFail()();
    }
    }-*/;

	

	
	public Person createPerson(String name, String picture, String id) {
		Person person = new Person();
		person.setName(name);
		person.setPicture(picture);
		person.setId(id);
		return person;
	}
	
	public void onFail(){
		Window.alert("Fail");
	}

	
	public List getNewList(){
		return new ArrayList();
	}
	
	public void addPerson(List listPerson, Person person){
		listPerson.add(person);
	}
	

	public void onGetFriends(List listPerson){
	}
	
	
	/***********************************
	 * 
	 * Populate
	 * 
	 * **/
	
	
	public native void updateViewerAppData(String field, String data)/*-{

	    var req = $wnd.opensocial.newDataRequest();
  	    req.add(req.newUpdatePersonAppDataRequest("VIEWER", field, data));
	  
	    req.send(this.@gopensocial.client.GOpenSocial::onLoadUpdateViewerAppData(Lcom/google/gwt/core/client/JavaScriptObject;), "update_data");
    }-*/;

	
	public native void onLoadUpdateViewerAppData(JavaScriptObject data) /*-{
	    var singleton = @gopensocial.client.GOpenSocial::getInstance()();
	
        if(data.hadError()){
           singleton.@gopensocial.client.GOpenSocial::onFail()();
        }
    }-*/;

	/*
	 * 
	 * 
	 * Get Data
	 * 
	 */
	public native void getViewerAppData(String field)/*-{

       var req = $wnd.opensocial.newDataRequest();
       var fields = [ field ];
	   req.add(req.newFetchPersonAppDataRequest("VIEWER", fields), "viewer_data");
  
       req.send(this.@gopensocial.client.GOpenSocial::onLoadGetViewerAppData(Lcom/google/gwt/core/client/JavaScriptObject;));
    }-*/;

	public native void getPersonAppData(String field)/*-{

    var req = $wnd.opensocial.newDataRequest();
    var fields = [ field ];
	   req.add(req.newFetchPersonAppDataRequest("OWNER_FRIENDS", fields), "viewer_data");

    req.send(this.@gopensocial.client.GOpenSocial::onLoadGetViewerAppData(Lcom/google/gwt/core/client/JavaScriptObject;));
 }-*/;


    public native void onLoadGetViewerAppData(JavaScriptObject data) /*-{
       var singleton = @gopensocial.client.GOpenSocial::getInstance()();
       
       if(!data.hadError()){
          var mydata = data.get("viewer_data").getData();
          for (id in mydata) {
             var fields = mydata[id];
             for (field in fields){
               var value = fields[field];
               singleton.@gopensocial.client.GOpenSocial::onGetViewerAppData(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(id, field, value);
             }
          }
       }
       else {
          singleton.@gopensocial.client.GOpenSocial::onFail()();
       }
     }-*/;
    
    public void onGetViewerAppData(String id, String field, String value){

    }
}
